//
//  ViewController.swift
//  HttpStydu
//
//  Created by yongkang on 2017/8/10.
//  Copyright © 2017年 sheng. All rights reserved.
//

import UIKit
import Alamofire
import AlamofireObjectMapper
import SwiftyJSON
import SDWebImage

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
    
    var newdatas:[NewsData] = []
    
    var news:[News] = []
    
    @IBOutlet weak var tabView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //getNewsData()

        //getNewsdata()
        
        
        AsyncHttp.HttpRequest.getHomeData(page: 1) {
            (newItems) -> () in
            
            self.news = newItems
            self.tabView.reloadData()
        }
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
        
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("数量： \(news.count)")
        //return newdatas.count
        return news.count
    }
    
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tabView.dequeueReusableCell(withIdentifier: "news")!
        
        //let newdata = newdatas[indexPath.row] as NewsData
        let newdata = news[indexPath.row] as News
        
        let newsImage = cell.viewWithTag(1) as! UIImageView
        let title = cell.viewWithTag(2) as! UILabel
        let type = cell.viewWithTag(3) as! UILabel
        let comment = cell.viewWithTag(4) as! UILabel
    
        
        //newImage.image = UIImage(named: newdata.newsImage!)
        title.text = newdata.newsTitle!
        
        type.text = newdata.newsCategory!
        comment.text = "评论数：" + String(describing: newdata.commentCount!)
        
        newsImage.sd_setImage(with: URL(string: newdata.newsImage!), placeholderImage: UIImage(named: "1.png"))
            
        return cell
    }
    
    
    
    func getNewsData() {
        let url = "http://a.xcar.com.cn/interface/6.0/getNewsList.php"
        
        let params = ["limit": 100,
                      "offset": 0,
                      "type": 2]
        
        Alamofire.request(url, parameters: params).responseJSON { response in
            
            guard response.result.isSuccess else {
                return
            }
            if let value = response.result.value {
                //print("JSON: \(value)")
                let dict = JSON(value)
                
                if let newsLists = dict["newsList"].arrayObject {
                    for newsList in  newsLists {
                        
                        let yp_newsList = NewsData(dict: newsList as! [String: AnyObject])
                        self.newdatas.append(yp_newsList)
                        
                    }
                }
                self.tabView.reloadData()
            }
        }
    }
    
    
    
    func getNewsdata() {
        let url = "http://a.xcar.com.cn/interface/6.0/getNewsList.php"
        
        let params = ["limit": 200,
                      "offset": 0,
                      "type": 2]
        
        Alamofire.request(url, method: .post, parameters: params).responseObject { (response: DataResponse<NewsMode>) in
            
            guard response.result.isSuccess else {
                return
            }
            let newMode = response.result.value
            if let item = newMode?.newsList {
                self.news = item
                self.tabView.reloadData()
            }
        }
    }
    
        
    //cell点击跳转事件
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let identifier = segue.identifier,
            let indexPath = self.tabView.indexPathForSelectedRow,
            identifier == "showDetail" {
            let controller = segue.destination as! DetailController
            controller.url = news[indexPath.row].newsLink
        }
    }
    
    


}

